package com.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;
import com.opensymphony.xwork2.ModelDriven;

import com.po.Menu;
import com.po.UserSys;
import com.action.BaseAction;
import com.common.Tools;

import com.dao.IRoleDao;
import com.dao.IUserDao;



public class UserAction extends BaseAction implements ModelDriven<UserSys> {
	
	UserSys form = new UserSys();
	public UserSys getModel() {
		return form;
	}
	private IUserDao userDao;
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	private IRoleDao roleDao;
	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public String login(){
		String authCode=(String)getSession().getAttribute("AUTH_CODE");
		if(authCode==null||"".equals(authCode)){
			form.setReturnInfo("验证码不正确");
			form.setReturnCode(0);
			return SUCCESS;
		}
		if(form.getAuthCode()!=null&&form.getAuthCode().toUpperCase().equals(authCode.toUpperCase())){	
			UserSys _user = userDao.login(form);
			if(_user!=null){
				getSession().setAttribute("User", _user);
				form.setReturnCode(1);				
				return SUCCESS;
			}else{
				form.setReturnInfo("用户密码不正确,或账户已禁用！");
				form.setReturnCode(0);
				return SUCCESS;
			}
		}else{
			form.setReturnInfo("验证码不正确");
			form.setReturnCode(0);
			return SUCCESS;
		}		
	}   
	
	public String logined(){
		UserSys _user = (UserSys)getSession().getAttribute("User");
		form.setUserid(_user.getUserid());
		List<Menu> menulist = userDao.findRoleMenu(form);
		List<Menu> menuchildlist = new ArrayList<Menu>();
		for(Menu menu : menulist){
			if(menu.getMenutype()==3){
				menuchildlist.add(menu);
			}
		}
		getSession().setAttribute("menulist", menuchildlist);
		form.setMenulist(menulist);		
		return SUCCESS;
	}
	
	public String logout(){
		getSession().removeAttribute("User");
		form.setReturnCode(111);
		return SUCCESS;
	}		
		
	
	
	
	
	
}
